package com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.SmsEnrollmentManagementCollegeMajor;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.SmsEnrollmentManagementTask;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.req.SmsEnrollmentManagementTaskReq;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.resp.SmsEnrollmentManagementTaskResp;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.mapper.SmsEnrollmentManagementCollegeMajorMapper;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.mapper.SmsEnrollmentManagementTaskMapper;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.service.SmsEnrollmentManagementTaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.constant.TaskStatusConstant.TASK_STATUS_CLOSE;
import static com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.constant.TaskStatusConstant.TASK_STATUS_OPEN;

/**
 * <p>
 * 招生计划表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-15
 */
@Service
public class SmsEnrollmentManagementTaskServiceImpl extends ServiceImpl<SmsEnrollmentManagementTaskMapper, SmsEnrollmentManagementTask> implements SmsEnrollmentManagementTaskService {
    @Resource
    private SmsEnrollmentManagementTaskMapper smsEnrollmentManagementTaskMapper;
    @Resource
    private SmsEnrollmentManagementCollegeMajorMapper smsEnrollmentManagementCollegeMajorMapper;


    @Override
    public ResponseResult addEnrollmentManagementTask(SmsEnrollmentManagementTaskReq enrollmentManagementTaskReq, String user_id) {
        if (isNullOrEmpty(enrollmentManagementTaskReq.getTaskName(), enrollmentManagementTaskReq.getTaskBgTime(),
                enrollmentManagementTaskReq.getTaskEndTime())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (enrollmentManagementTaskReq.getTaskEndTime().getTime() - enrollmentManagementTaskReq.getTaskBgTime().getTime() < 0) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsEnrollmentManagementTask addTask = new SmsEnrollmentManagementTask();
        addTask.setTaskName(enrollmentManagementTaskReq.getTaskName());
        addTask.setTaskBgTime(enrollmentManagementTaskReq.getTaskBgTime());
        addTask.setTaskEndTime(enrollmentManagementTaskReq.getTaskEndTime());
        addTask.setTaskStatus(TASK_STATUS_CLOSE);
        addTask.setTaskNum(0);
        addTask.setCreateUser(user_id);
        smsEnrollmentManagementTaskMapper.insert(addTask);
        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult deleteEnrollmentManagementTask(SmsEnrollmentManagementTaskReq enrollmentManagementTaskReq, String user_id) {
        //TODO  删除计划下院系计划  删除计划关联学生
        if (isNullOrEmpty(enrollmentManagementTaskReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        smsEnrollmentManagementCollegeMajorMapper.delete(new LambdaQueryWrapper<SmsEnrollmentManagementCollegeMajor>()
                .eq(SmsEnrollmentManagementCollegeMajor::getTaskId,enrollmentManagementTaskReq.getId()));

        smsEnrollmentManagementTaskMapper.deleteById(enrollmentManagementTaskReq.getId());

        return CommonResult.success();
    }

    @Override
    public ResponseResult updateEnrollmentManagementTask(SmsEnrollmentManagementTaskReq enrollmentManagementTaskReq, String user_id) {
        if (isNullOrEmpty(enrollmentManagementTaskReq.getTaskName(), enrollmentManagementTaskReq.getTaskBgTime(),
                enrollmentManagementTaskReq.getTaskEndTime(), enrollmentManagementTaskReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (enrollmentManagementTaskReq.getTaskEndTime().getTime() - enrollmentManagementTaskReq.getTaskBgTime().getTime() < 0) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsEnrollmentManagementTask smsEnrollmentManagementTask = smsEnrollmentManagementTaskMapper.selectById(enrollmentManagementTaskReq.getId());
        if (smsEnrollmentManagementTask == null) {
            return CommonResult.failed(CommonCodeEnum.ENROLLMENT_MANAGEMENT_TASK_NOT_EXIST);
        }

        SmsEnrollmentManagementTask updateTask = new SmsEnrollmentManagementTask();
        updateTask.setId(enrollmentManagementTaskReq.getId());
        updateTask.setTaskName(enrollmentManagementTaskReq.getTaskName());
        updateTask.setTaskBgTime(enrollmentManagementTaskReq.getTaskBgTime());
        updateTask.setTaskEndTime(enrollmentManagementTaskReq.getTaskEndTime());
        smsEnrollmentManagementTaskMapper.updateById(updateTask);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getEnrollmentManagementTaskList(String searchStr, Integer pageNum, Integer pageSize) {
        QueryWrapper<SmsEnrollmentManagementTask> queryWrapper = new QueryWrapper<>();
        if (!isNullOrEmpty(searchStr)) {
            queryWrapper.lambda().like(SmsEnrollmentManagementTask::getTaskName, searchStr);
        }
        queryWrapper.lambda().orderByDesc(SmsEnrollmentManagementTask::getTaskStatus)
                .orderByDesc(SmsEnrollmentManagementTask::getCreateTime);

        Page<SmsEnrollmentManagementTask> page = new Page<>(pageNum,pageSize);
        smsEnrollmentManagementTaskMapper.selectPage(page,queryWrapper);

        List<SmsEnrollmentManagementTaskResp> list = page.getRecords()
                .stream()
                .map(task->{
                    SmsEnrollmentManagementTaskResp taskResp = new SmsEnrollmentManagementTaskResp();
                    BeanUtils.copyProperties(task,taskResp);
                    return taskResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list,total);
    }

    @Override
    public ResponseResult switchEnrollmentManagementTask(SmsEnrollmentManagementTaskReq enrollmentManagementTaskReq) {
        if (isNullOrEmpty(enrollmentManagementTaskReq.getTaskStatus(),enrollmentManagementTaskReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (!enrollmentManagementTaskReq.getTaskStatus().equals(TASK_STATUS_OPEN) && !enrollmentManagementTaskReq.getTaskStatus().equals(TASK_STATUS_CLOSE)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsEnrollmentManagementTask smsEnrollmentManagementTask = smsEnrollmentManagementTaskMapper.selectById(enrollmentManagementTaskReq.getId());
        if (smsEnrollmentManagementTask == null) {
            return CommonResult.failed(CommonCodeEnum.ENROLLMENT_MANAGEMENT_TASK_NOT_EXIST);
        }

        if (smsEnrollmentManagementTask.getTaskStatus().equals(enrollmentManagementTaskReq.getTaskStatus())){
            return CommonResult.success();
        }

        //开启 关闭其他计划
        if (enrollmentManagementTaskReq.getTaskStatus().equals(TASK_STATUS_OPEN)){
            SmsEnrollmentManagementTask updateOther = new SmsEnrollmentManagementTask();
            updateOther.setTaskStatus(TASK_STATUS_CLOSE);
            smsEnrollmentManagementTaskMapper.update(updateOther,new LambdaQueryWrapper<SmsEnrollmentManagementTask>()
                    .eq(SmsEnrollmentManagementTask::getTaskStatus,TASK_STATUS_OPEN));
        }

        SmsEnrollmentManagementTask updateTask = new SmsEnrollmentManagementTask();
        updateTask.setId(enrollmentManagementTaskReq.getId());
        updateTask.setTaskStatus(enrollmentManagementTaskReq.getTaskStatus());
        smsEnrollmentManagementTaskMapper.updateById(updateTask);
        return CommonResult.success();
    }
}
